%% initialisierung der Variablen
D = dir('testset/*.gif');
I = cell(120,1);
NAMES = cell(120,1);
BWL = cell(120,1);
STATS = cell(120,1);

%Real Features
AREA = zeros(120,1);
PERIMETER = zeros(120,1);
LONG = zeros(120,1);
CONVEX = zeros(120,1);

FORMFACTOR = zeros(120,1);
ROUNDNESS = zeros(120,1);
COMPACTNESS = zeros(120,1);
ASPECT = zeros(120,1);
SOLIDITY = zeros(120,1);
ECCENTRICITY = zeros(120,1);

%% fuer jede Datei in Verzeichnis D=dir
j = 1;
for i = 1 : size(D, 1)                     % fuer jede Datei in Verzeichnis D=dir
    if D(i).isdir==0 % && strncmp(D(i).name(1:3), 'ele', 3) || strncmp(D(i).name(1:3), 'sea', 3) || strncmp(D(i).name(1:9), 'horseshoe', 9) || strncmp(D(i).name(1:3), 'lmf', 3) || strncmp(D(i).name(1:3), 'per', 3)) % speichere Datei mit uebergebenen drei Anfangbuchstaben
        disp(D(i).name);
        NAMES{j} = D(i).name;
        I{j} = double(imread(strcat('img',filesep,D(i).name)));
        j = j+1;
    end
end

%% zusammenhaengende Pixel in "beschilderte" Regionen unterteilt
for i = 1 : size(I, 1)
    
    BWL{i} = bwlabel(I{i});                 % Etikettierung (labeling) der verbundenen Regionen
    tmp = regionprops(BWL{i}, 'Area');
    a = [tmp.Area];
    [m id] = max(a(:)); % Finden der id der groessten Region
    L = BWL{i};
    L(find(bwlabel(L) ~= id)) = 0;
    BWL{i} = L; % Die groesste Region wird weiter verwendet, alle anderen werden ignoriert

    STATS{i} = regionprops(BWL{i},'all'); % Berechne alle Image Features auf Basis der etikettierten Regionen
    STATS{i} = STATS{id,1}; % Sicherstellen, dass nur die Werte der groessten Region verwendet werden.
    
    temp_stats = regionprops(BWL{i},'all'); % Berechne alle Image Features auf Basis der etikettierten Regionen
    STATS{i} = temp_stats(id);
    
    temp = size(I{i});                      % speichere in temp Groesseninformation des Bildes
    AREA(i,1) = STATS{i}.Area / (temp(1)*temp(2)); % berechne Objektfleache durch Gesamtflaeche (=Breite mal Hoehe)
    
    ASPECT(i,1) = STATS{i}.MajorAxisLength / STATS{i}.MinorAxisLength; % Berechne Aspect Ratio
    
    %Berechnen der relativen "Breite" der Region
    temp = (BWL{i}==1);
    temp2 = (temp==0);
    temp3 = bwdist(temp2);                  % Distanztransformation
    width= max(max(temp3));                 % speichere maximale Ausdehnung nach Distanztransformation = Breite in width
    area = STATS{i}.Area;                   % berechne und speichere Anzahl der Region-Pixel in area
    LONG(i,1) = (width^2) / area;             % Verhaeltnisberechnung Objektleange zu Bildfleache
    
    %Berechnen des Umfanges der Bounding Box in Relation zum Umfang der
    %Region
    temp = (BWL{i}==1);
    perimeter = STATS{i}.Perimeter;         % berechne Umfang
    bbox = STATS{i}.BoundingBox;          % speiche BoundingBoxArray                                       
    perimeter_box = (bbox(3) + bbox(4)) * 2;% 3. und 4.Element = Breite und Hoehe der Bounding Box
    PERIMETER(i,1) = perimeter / perimeter_box; 
    
    %Calculate Shape Descriptors
    FORMFACTOR(i,1) = (4*pi*area)/(perimeter^2);
    ROUNDNESS(i,1) = (4*area)/(pi*STATS{i}.MajorAxisLength^2);
    COMPACTNESS(i,1) = sqrt(ROUNDNESS(i,1));
    SOLIDITY(i,1) = STATS{i}.Solidity; 
    ECCENTRICITY(i,1) = STATS{i}.Eccentricity; % Die relation zwischen dem Abstand der Brennpunkte der best-fit Ellipse und der Laenge der Hauptachse.
    
    convex = STATS{i}.ConvexImage;
    area_convex = sum(sum(convex));
    area = STATS{i}.Area;
    CONVEX(i,1) = area_convex / area; 
end

%% Graph fuer die normalisierte Flaeche
%x=1:100;
%y=AREA;
%figure; bar(x,y); title 'Normalized Area';
%xticklabel_rotate(1:100,90,NAMES,'interpreter','none');

figure; plot(1:20, AREA(1:20,1), '-ro', 1:20, AREA(21:40,1), '-.bd', 1:20, AREA(41:60,1), '--gx', 1:20, AREA(61:80,1), ':k+', 1:20, AREA(81:100,1), '-c*', 1:20, AREA(101:120,1), '-mx'); 
title 'Normalized Area 2';
h = legend ('Elephant', 'Horseshoe', 'Lmfish', 'Personal Car', 'Sea Snake', 'Other');
set(h,'Interpreter','none')

%% Graph fuer den Umfang in Relation zum Umfang der BoundingBox
figure; plot(1:20, PERIMETER(1:20,1), '-ro', 1:20, PERIMETER(21:40,1), '-.bd', 1:20, PERIMETER(41:60,1), '--gx', 1:20, PERIMETER(61:80,1), ':k+', 1:20, PERIMETER(81:100,1), '-c*', 1:20, PERIMETER(101:120,1), '-mx'); 
title 'Region Umfang/BoundingBox Umfang 2';
h = legend ('Elephant', 'Horseshoe', 'Lmfish', 'Personal Car', 'Sea Snake', 'Other');
set(h,'Interpreter','none')

%% Graph fuer die Laenglichkeit
figure; plot(1:20, LONG(1:20,1), '-ro', 1:20, LONG(21:40,1), '-.bd', 1:20, LONG(41:60,1), '--gx', 1:20, LONG(61:80,1), ':k+', 1:20, LONG(81:100,1), '-c*', 1:20, LONG(101:120,1), '-mx'); 
title 'Laenglichkeit';
h = legend ('Elephant', 'Horseshoe', 'Lmfish', 'Personal Car', 'Sea Snake', 'Other');
set(h,'Interpreter','none')

%% Graph fuer die Convexity
figure; plot(1:20, CONVEX(1:20,1), '-ro', 1:20, CONVEX(21:40,1), '-.bd', 1:20, CONVEX(41:60,1), '--gx', 1:20, CONVEX(61:80,1), ':k+', 1:20, CONVEX(81:100,1), '-c*', 1:20, CONVEX(101:120,1), '-mx'); 
title 'Convexity';
h = legend ('Elephant', 'Horseshoe', 'Lmfish', 'Personal Car', 'Sea Snake', 'Other');
set(h,'Interpreter','none')

%% Bar plot fuer das aspect ratio
%x=1:100;
%y=ASPECT;
%figure; bar(x,y); title 'Aspect Ratio'
%xticklabel_rotate(1:100,90,NAMES,'interpreter','none');

figure; plot(1:20, ASPECT(1:20,1), '-ro', 1:20, ASPECT(21:40,1), '-.bd', 1:20, ASPECT(41:60,1), '--gx', 1:20, ASPECT(61:80,1), ':k+', 1:20, ASPECT(81:100,1), '-c*', 1:20, ASPECT(101:120,1), '-mx'); 
title 'Aspect Ratio 2';
h = legend ('Elephant', 'Horseshoe', 'Lmfish', 'Personal Car', 'Sea Snake', 'Other');
set(h,'Interpreter','none')

%% Graph fuer den Formfactor
%x=1:100;
%y=FORMFACTOR;
%figure; bar(x,y); title 'Formfactor'
%xticklabel_rotate(1:100,90,NAMES,'interpreter','none');

figure; plot(1:20, FORMFACTOR(1:20,1), '-ro', 1:20, FORMFACTOR(21:40,1), '-.bd', 1:20, FORMFACTOR(41:60,1), '--gx', 1:20, FORMFACTOR(61:80,1), ':k+', 1:20, FORMFACTOR(81:100,1), '-c*', 1:20, FORMFACTOR(101:120,1), '-mx'); 
title 'Formfactor 2';
h = legend ('Elephant', 'Horseshoe', 'Lmfish', 'Personal Car', 'Sea Snake', 'Other');
set(h,'Interpreter','none')

%% Graph fuer die Roundness
%x=1:100;
%y=ROUNDNESS;
%figure; bar(x,y); title 'Roundness'
%xticklabel_rotate(1:100,90,NAMES,'interpreter','none');

figure; plot(1:20, ROUNDNESS(1:20,1), '-ro', 1:20, ROUNDNESS(21:40,1), '-.bd', 1:20, ROUNDNESS(41:60,1), '--gx', 1:20, ROUNDNESS(61:80,1), ':k+', 1:20, ROUNDNESS(81:100,1), '-c*', 1:20, ROUNDNESS(101:120,1), '-mx'); 
title 'Roundness 2';
h = legend ('Elephant', 'Horseshoe', 'Lmfish', 'Personal Car', 'Sea Snake', 'Other');
set(h,'Interpreter','none')

%% Graph fuer die Compactness
%x=1:100;
%y=COMPACTNESS;
%figure; bar(x,y); title 'Compactness'
%xticklabel_rotate(1:100,90,NAMES,'interpreter','none');

figure; plot(1:20, COMPACTNESS(1:20,1), '-ro', 1:20, COMPACTNESS(21:40,1), '-.bd', 1:20, COMPACTNESS(41:60,1), '--gx', 1:20, COMPACTNESS(61:80,1), ':k+', 1:20, COMPACTNESS(81:100,1), '-c*', 1:20, COMPACTNESS(101:120,1), '-mx'); 
title 'Compactness 2';
h = legend ('Elephant', 'Horseshoe', 'Lmfish', 'Personal Car', 'Sea Snake', 'Other');
set(h,'Interpreter','none')

%% Graph fuer die Solidity
%x=1:100;
%y=SOLIDITY;
%figure; bar(x,y); title 'Solidity'
%xticklabel_rotate(1:100,90,NAMES,'interpreter','none');

figure; plot(1:20, SOLIDITY(1:20,1), '-ro', 1:20, SOLIDITY(21:40,1), '-.bd', 1:20, SOLIDITY(41:60,1), '--gx', 1:20, SOLIDITY(61:80,1), ':k+', 1:20, SOLIDITY(81:100,1), '-c*', 1:20, SOLIDITY(101:120,1), '-mx'); 
title 'Solidity 2';
h = legend ('Elephant', 'Horseshoe', 'Lmfish', 'Personal Car', 'Sea Snake', 'Other');
set(h,'Interpreter','none')

%% Graph fuer die Solidity ECCENTRICITY
figure; plot(1:20, ECCENTRICITY(1:20,1), '-ro', 1:20, ECCENTRICITY(21:40,1), '-.bd', 1:20, ECCENTRICITY(41:60,1), '--gx', 1:20, ECCENTRICITY(61:80,1), ':k+', 1:20, ECCENTRICITY(81:100,1), '-c*', 1:20, ECCENTRICITY(101:120,1), '-mx'); 
title 'Eccentricity';
h = legend ('Elephant', 'Horseshoe', 'Lmfish', 'Personal Car', 'Sea Snake', 'Other');
set(h,'Interpreter','none')